package com.cn.kabalifeuser.ui.login;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.base.MyApplication;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.MainActivity;
import com.cn.kabalifeuser.ui.login.bean.AgreementBean;
import com.cn.kabalifeuser.ui.login.bean.LoginBean;
import com.cn.kabalifeuser.ui.login.bean.LoginLibBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.login.bean.RegisterBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.SPUtil;
import com.cn.kabalifeuser.utils.StringUtil;
import com.flyco.roundview.RoundTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目：KaBaLifeUser
 * 包名：com.cn.kabalifeuser.ui.login
 * 类名：PhoneActivity
 * 创建时间：2020/11/18 9:24 AM
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
public class PhoneActivity extends IBaseActivity<LoginView, LoginPresenter> implements LoginView {
    @BindView(R.id.et_register_phone)
    EditText etRegisterPhone;
    @BindView(R.id.et_register_code)
    EditText etRegisterCode;
    @BindView(R.id.tv_btn_register_code)
    RoundTextView tvBtnRegisterCode;

    int signMsg = 1;//1是登录。2的注册
    MyCountDownTimer myCountDownTimer;

    Bundle bundle;
    int LoginLibId;

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_bd_phone;
    }

    @Override
    public void onReLoggedIn(String msg) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public Context _getContext() {
        return getMe();
    }

    @Override
    public void onPhoneCodeSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
            myCountDownTimer.start();
    }

    @Override
    public void onLoginSuccess(LoginBean bean) {
        boolean is = SPUtil.saveObjectToShare(LoginHelper.USER_LOGIN, bean);
        if (is) {
            SPUtil.saveString(LoginHelper.ISLOGIN, "1");
            $startActivityFinish(MainActivity.class);
            MyApplication.getInstance().getActivityManager().popAllActivityExceptOne(MainActivity.class);

        }
    }

    @Override
    public void onRegisterSuccess(RegisterBean bean) {

    }

    @Override
    public void onLoginLibSuccess(LoginLibBean bean) {

    }

    @Override
    public void onAgreementSuccess(AgreementBean bean) {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("绑定手机");

        bundle=getIntent().getExtras();
        LoginLibId=bundle.getInt("LoginLibId");
        myCountDownTimer = new MyCountDownTimer(60000, 1000);
    }

    @OnClick({R.id.tv_btn_register_code, R.id.tv_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_register_code:
                //todo 注册 获取的 获取验证码
                if (!TextUtils.isEmpty(etRegisterPhone.getText().toString().trim())) {
                    signMsg = 2;
                    myCountDownTimer.cancel();
                    mPresenter.onPhoneCodeData(etRegisterPhone.getText().toString().trim(), 1);
                } else RxToast.error("请输入手机号码");

                break;
            case R.id.tv_btn_next:
                if (!TextUtils.isEmpty(etRegisterPhone.getText().toString().trim())) {
                    if (!TextUtils.isEmpty(etRegisterCode.getText().toString().trim())) {
                                        if (StringUtil.isChinaPhoneLegal(etRegisterPhone.getText().toString().trim())){
                                            mPresenter.onBindingPhoneData(
                                                    etRegisterPhone.getText().toString().trim(),
                                                    etRegisterCode.getText().toString().trim(), LoginLibId
                                            );
                                        }else RxToast.error("请输入正确的手机号 ");

                    } else RxToast.error("请输入验证码");
                } else RxToast.error("请输入手机号码");
                break;
        }
    }

    /**
     * 倒计时函数
     */
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击

                tvBtnRegisterCode.setClickable(false);
                tvBtnRegisterCode.setText(l / 1000 + getResources().getString(R.string.time_second_str));
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //设置可点击

                tvBtnRegisterCode.setText(getResources().getString(R.string.time_again_str));
                tvBtnRegisterCode.setClickable(true);

        }
    }
}
